#include <stdio.h>
#include <stdlib.h>

struct Point
{
    int x;
    int y;
};

struct Rectangle
{
    struct Point topLeft;
    struct Point bottomRight;
};

//计算矩形面积
int calculateArea(struct Rectangle* rect)
{
    int width = abs(rect->topLeft.x-rect->bottomRight.x);
    int height = abs(rect->topLeft.y-rect->bottomRight.y);
    return width * height;
}

//平移矩形
void moveRectangle(struct Rectangle* rect, int dx, int dy)
{
    rect->bottomRight.x += dx;
    rect->bottomRight.y += dy;
    rect->topLeft.x += dx;
    rect->topLeft.y += dy;
}

int isPointInside(struct Rectangle* rect, struct Point* p)
{
    if(p->x >= rect->topLeft.x && p->x <= rect->bottomRight.x && p->x <= rect->topLeft.y && p->x >= rect->bottomRight.y)
        return 1;
    return 0;
}

int main(int argc, char const *argv[])
{
    struct Point n = {3,1};
    struct Point m = {1,2};
    struct Point p = {3,2};

    struct Rectangle* rect = (struct Rectangle *)malloc(sizeof(struct Rectangle));

    if (rect == NULL) 
    {
        printf("内存分配失败！\n");
        return 1;
    }

    rect->bottomRight = n;
    rect->topLeft = m;

    printf("矩形的面积是：%d\n",calculateArea(rect));

    moveRectangle(rect,1,1);

    printf("平移后各点的位置：\n");
    printf("m=(%d,%d)  ",rect->topLeft.x,rect->topLeft.y);
    printf("n=(%d,%d)\n",rect->bottomRight.x,rect->bottomRight.y);

    int c = isPointInside(rect,&p);
    if(c == 1)
        printf("点p(%d,%d)在矩阵内\n",p.x,p.y);
    else
        printf("点p(%d,%d)不在矩阵内\n",p.x,p.y);

    free(rect);

    return 0;
}
